package com.sl.transport.mq;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.sl.transport.common.constant.Constants;
import com.sl.transport.entity.node.AgencyEntity;
import com.sl.transport.entity.node.BaseEntity;
import com.sl.transport.entity.node.OLTEntity;
import com.sl.transport.entity.node.TLTEntity;
import com.sl.transport.enums.OrganTypeEnum;
import com.sl.transport.mq.bean.AuthMQBean;
import com.sl.transport.service.IService;
import com.sl.transport.utils.OrganServiceFactory;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class AuthMQListener {

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = Constants.MQ.Queues.AUTH_TRANSPORT),
            exchange = @Exchange(name = "${rabbitmq.exchange}", type = ExchangeTypes.TOPIC),
            key = "#" //接收全部消息
    ))
    public void listenAgencyMsg(String msg) {
        //1. 解析消息，格式是json
        //{"type":"ORG","operation":"ADD","content":[{"id":"1282668564695744993","name":"测试1","parentId":"0","managerId":"1","status":true}]}
        // JSONObject jsonObject = JSONUtil.parseObj(msg);
        AuthMQBean authMQBean = JSONUtil.toBean(msg, AuthMQBean.class);

        if (ObjectUtil.notEqual(authMQBean.getType(), "ORG")) {
            //忽略消息
            return;
        }

        if (CollUtil.isEmpty(authMQBean.getContent())) {
            return;
        }

        //2. 根据不同的操作进行调用对应的service不同的方法
        authMQBean.getContent().forEach(content -> this.handMsg(authMQBean.getOperation(), content));

    }

    private void handMsg(String operation, AuthMQBean.Content content) {
        String name = content.getName();
        BaseEntity entity;
        IService iService;
        if (StrUtil.endWith(name, "转运中心")) {
            //一级转运中心
            entity = BeanUtil.toBean(content, OLTEntity.class);
            iService = OrganServiceFactory.getBean(OrganTypeEnum.OLT);
        } else if (StrUtil.endWith(name, "分拣中心")) {
            //二级转运中心
            entity = BeanUtil.toBean(content, TLTEntity.class);
            iService = OrganServiceFactory.getBean(OrganTypeEnum.TLT);
        } else if (StrUtil.endWith(name, "营业部")) {
            //网点
            entity = BeanUtil.toBean(content, AgencyEntity.class);
            iService = OrganServiceFactory.getBean(OrganTypeEnum.AGENCY);
        } else {
            return;
        }

        entity.setBid(content.getBid());

        switch (operation) {
            case "ADD": {
                iService.create(entity);
                break;
            }
            case "UPDATE": {
                iService.update(entity);
                break;
            }
            case "DEL": {
                iService.deleteByBid(entity.getBid());
                break;
            }
        }
    }

}
